# Copyright (C) 2007 by Sergio Pistone
# sergio_pistone@yahoo.com.ar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

require File.expand_path( File.dirname( __FILE__ ) + "/../mediawikilyrics" )
require File.expand_path( File.dirname( __FILE__ ) + "/pluginadapter" )
require File.expand_path( File.dirname( __FILE__ ) + "/wikipluginadapter" )

module Plugins

	@@ALL = []
	@@WIKI = []

	# Load lyrics plugins:

	plugins_dir = Dir.new( File.expand_path( File.dirname( __FILE__ ) + "/.." ) )
	plugins_dir.each() do |file_name|
		file_name = File.expand_path( File.dirname( __FILE__ ) + "/../" + file_name )
		next if ! File.file?( file_name ) || (md = /\/lyrics_([A-Za-z_0-9]*)\.rb$/.match( file_name )) == nil
		require( file_name )
		class_name = md[1]
		class_obj = eval( class_name )
		if class_obj.ancestors().include?( MediaWikiLyrics )
			eval( "class Amarok#{class_name} < #{class_name}\ninclude WikiPluginAdapter\nend" )
			plugin = eval( "Amarok#{class_name}.new()" )
			@@WIKI << plugin
		else
			eval( "class Amarok#{class_name} < #{class_name}\ninclude PluginAdapter\nend" )
			plugin = eval( "Amarok#{class_name}.new()" )
		end
		@@ALL << plugin
	end

	@@ALL.sort!() { |x,y| x.site_name <=> y.site_name }
	@@WIKI.sort!() { |x,y| y.site_name() <=> x.site_name() }
	@@USED = @@WIKI + (@@ALL - @@WIKI)

	def Plugins.all_plugins()
		return @@ALL
	end

	def Plugins.all_names()
		return @@ALL.collect(){ |plugin| plugin.plugin_name() }
	end

	def Plugins.used_plugins()
		return @@USED
	end

	def Plugins.used_names()
		return @@USED.collect(){ |plugin| plugin.plugin_name() }
	end

	def Plugins.used_names=( names )
		@@USED = []
		names.uniq().each() do |name|
			@@ALL.each do |plugin|
				if plugin.plugin_name() == name
					@@USED << plugin
					break
				end
			end
		end
	end

	def Plugins.wiki_plugins()
		return @@WIKI
	end

	def Plugins.wiki_names()
		return @@WIKI.collect(){ |plugin| plugin.plugin_name() }
	end

	def Plugins.plugin_by_name( name )
		@@ALL.each() do |plugin|
			return plugin if plugin.plugin_name() == name
		end
		return nil
	end

end
